package me.feng.util

import net.sf.ehcache.CacheManager
import me.feng.entity.User
import net.sf.ehcache.Element
import java.util.{ List => JList }
import scala.collection.JavaConverters._
import scala.collection.JavaConversions._

object CacheUtil {

  val manager = CacheManager.create()

  val oluserCache = manager.getCache("OlUserCache")

  def getOlUser(uid: String): User = {
    val o = oluserCache.get(uid)
    if (o == null) null else o.getValue().asInstanceOf[User]
  }

  def addOlUser(u: User) = {
    u.online = true
    oluserCache.put(new Element(u.id, u))
  }

  def removeUser(uid: String) = {
    oluserCache.remove(uid)
  }

  def getAllOlUser(): List[String] = {
    oluserCache.getKeys().asInstanceOf[JList[String]].toList
  }
}